<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the PDODB_Listener_Chain class.
 *
 * PHP version 5
 *
 * This file is part of PEAR::PDODB - A database abstraction layer for PHP5 built
 * using PDO. PHP version 5.1+ is recommended.
 *
 * PEAR::PDODB is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * PEAR::PDODB is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with PEAR::PDODB. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     PDODB
 * @category    Database
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Chain.php 2 2007-11-27 17:20:29Z mjijackson $
 * @link        http://pear.php.net/packages/PDODB
 */

/**
 * Require PDODB_Listener
 */
require_once dirname(__FILE__) . '.php';

/**
 * A chain class for PDODB event listeners. A chain contains multiple listeners,
 * each of which is fired when a chain event is fired, and may be used to enable
 * multiple listeners on a connection, instead of just one.
 *
 * @package     PDODB
 * @category    Database
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class PDODB_Listener_Chain implements PDODB_Listener
{

    protected $listeners = array();

    /**
     * Adds a listener to this chain of listeners.
     *
     * @param   PDODB_Listener  $listener       The listener to add
     * @return  bool                            True on success, false otherwise
     * @access  public
     */
    public function addListener(PDODB_Listener $listener)
    {
        if (!in_array($listener, $this->listeners, true)) {
            $this->listeners[] = $listener;
            return true;
        }

        return false;
    }

    /**
     * Removes a listener from this chain of listeners.
     *
     * @param   PDODB_Listener  $listener       The listener to remove
     * @return  bool                            True on success, false otherwise
     * @access  public
     */
    public function removeListener(PDODB_Listener $listener)
    {
        $ret = false;

        foreach ($this->listeners as $index => $l) {
            if ($listener === $l) {
                unset($this->listeners[$index]);
                $ret = true;
            }
        }

        return $ret;
    }

    /**
     * @see     PDODB_Listener::onWarning()
     */
    public function onWarning(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->onWarning($conn);
        }
    }

    /**
     * @see     PDODB_Listener::preConnect()
     */
    public function preConnect(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->preConnect($conn);
        }
    }

    /**
     * @see     PDODB_Listener::postConnect()
     */
    public function postConnect(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->postConnect($conn);
        }
    }

    /**
     * @see     PDODB_Listener::preDisconnect()
     */
    public function preDisconnect(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->preDisconnect($conn);
        }
    }

    /**
     * @see     PDODB_Listener::postDisconnect()
     */
    public function postDisconnect(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->postDisconnect($conn);
        }
    }

    /**
     * @see     PDODB_Listener::preQuery()
     */
    public function preQuery(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->preQuery($conn);
        }
    }

    /**
     * @see     PDODB_Listener::postQuery()
     */
    public function postQuery(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->postQuery($conn);
        }
    }

    /**
     * @see     PDODB_Listener::preExec()
     */
    public function preExec(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->preExec($conn);
        }
    }

    /**
     * @see     PDODB_Listener::postExec()
     */
    public function postExec(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->postExec($conn);
        }
    }

    /**
     * @see     PDODB_Listener::prePrepare()
     */
    public function prePrepare(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->prePrepare($conn);
        }
    }

    /**
     * @see     PDODB_Listener::postPrepare()
     */
    public function postPrepare(PDODB_Connection $conn)
    {
        foreach ($this->listeners as $listener) {
            $listener->postPrepare($conn);
        }
    }

}

?>
